<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="姓名" prop="UserName" v-if="entity.State == 1">
          <a-input v-model="entity.UserName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="金额" prop="Amount" v-if="entity.State == 1">
          <a-input v-model="entity.Amount" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="评分" prop="Score" v-if="entity.State == 3">
          <!-- <a-input v-model="entity.Score" autocomplete="off" /> -->
          <star @starMarkChange="onStarChange" />
        </a-form-model-item>
        <a-form-model-item label="评价描述" prop="Describe" v-if="entity.State == 3">
          <a-input v-model="entity.Describe" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import store from '@/store'
import Star from '@/components/Star/Star'

var requestItem = {}

export default {
  components: {
    Star,
  },
  props: {
    parentObj: Object,
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      rules: {
        Name: [{ required: true, message: '必填' }],
      },
      rating: 0,
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(item, title) {
      this.init()
      if (item) {
        requestItem = item
        this.entity = item
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        
        if (this.entity.State === 3) {
          this.loading = true
          console.log(this.entity)
          if (this.entity.Score) {
            requestItem.Source = this.entity.Score
          } else {
            requestItem.Source = 0
          }
          if (this.entity.Describe) {
            requestItem.ApprovalOpinion = this.entity.Describe
          } else {
            requestItem.ApprovalOpinion = ''
          }

          console.log(requestItem)
          this.$http.post('/GBT_Manage/GBT_ServiceRequest/Evaluate', requestItem).then((resJson) => {
            this.loading = false

            if (resJson.Success) {
              this.$message.success('操作成功!')
              this.visible = false

              this.parentObj.getDataList()
            } else {
              this.$message.error(resJson.Msg)
            }
          })
        } else {
          this.visible = false
        }
      })
    },
    onStarChange(e) {
      console.log(e)
      this.entity.Score = e
    },
  },
}
</script>
